package com.example.file_management.service;

import com.example.file_management.mapper.material.TaskMapper;
import com.example.file_management.pojo.material.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TaskService {

    private static final Logger logger = LoggerFactory.getLogger(TaskService.class);

    private final TaskMapper taskMapper;

    @Autowired
    public TaskService(TaskMapper taskMapper) {
        this.taskMapper = taskMapper;
    }

    // 插入任务
    @Transactional
    public int insertTask(Task task) {
        if (task == null) {
            throw new IllegalArgumentException("任务对象不能为空");
        }
        if (task.getTaskName() == null || task.getTaskName().trim().isEmpty()) {
            throw new IllegalArgumentException("任务名称不能为空");
        }
        if (task.getTaskDescription() == null || task.getTaskDescription().trim().isEmpty()) {
            throw new IllegalArgumentException("任务描述不能为空");
        }
        if (task.getTaskEndTime() == null) {
            throw new IllegalArgumentException("任务截止日期不能为空");
        }
        if (task.getTaskStatus() == null) {
            task.setTaskStatus(Task.TaskStatus.NotStarted); // 默认状态
        }

        try {
            int result = taskMapper.insertTask(task);
            logger.info("任务插入成功, 任务ID: {}", task.getTaskId());
            return result;
        } catch (Exception e) {
            logger.error("任务插入失败", e);
            throw new RuntimeException("任务插入失败", e);
        }
    }

    // 根据任务ID查询任务
    public Task getTaskById(Long taskId) {
        if (taskId == null || taskId <= 0) {
            throw new IllegalArgumentException("任务ID无效");
        }
        return taskMapper.getTaskById(Math.toIntExact(taskId));
    }

    // 查询所有任务
    public List<Task> getAllTasks() {
        return taskMapper.getAllTasks();
    }

    // 更新任务
    @Transactional
    public int updateTask(Task task) {
        if (task == null || task.getTaskId() == null || task.getTaskId() <= 0) {
            throw new IllegalArgumentException("任务ID不能为空");
        }

        try {
            int result = taskMapper.updateTask(task);
            if (result > 0) {
                logger.info("任务更新成功, 任务ID: {}", task.getTaskId());
            } else {
                logger.warn("任务更新失败, 任务ID: {}", task.getTaskId());
            }
            return result;
        } catch (Exception e) {
            logger.error("任务更新失败", e);
            throw new RuntimeException("任务更新失败", e);
        }
    }

    // 根据任务ID删除任务
    @Transactional
    public int deleteTask(Long taskId) {
        if (taskId == null || taskId <= 0) {
            throw new IllegalArgumentException("任务ID无效");
        }

        try {
            int result = taskMapper.deleteTask(Math.toIntExact(taskId));
            if (result > 0) {
                logger.info("任务删除成功, 任务ID: {}", taskId);
            } else {
                logger.warn("任务删除失败, 任务ID: {}", taskId);
            }
            return result;
        } catch (Exception e) {
            logger.error("任务删除失败", e);
            throw new RuntimeException("任务删除失败", e);
        }
    }
}
